package com.vancouver.knows.ui.activity.userpost;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.task.mark.GetUserPostsTaskMark;
import com.vancouver.knows.business.task.mark.QueryUserInfoTaskMark;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.util.CircleTransform;

/**
 * Created by Chris Li on 2016-07-25.
 */
public class ProfileUserPostListsView extends UserPostListsView implements View.OnClickListener {

    private RelativeLayout mUserHeaderView;
    private TextView mUserName;
    private ImageView mAvatar;
    private TextView mJoinDate;
    private TextView mPostCount;
    private TextView mFollowCount;


    public ProfileUserPostListsView(Context context, ARefreshViewController mController) {
        super(context, mController);
    }

    @Override
    protected void addListHeader(ListView listView) {
        super.addListHeader(listView);
        mUserHeaderView = (RelativeLayout) mInflater.inflate(R.layout.user_header_view, null, false);
        mUserHeaderView.findViewById(R.id.edit_profile).setVisibility(GONE);

        mUserName = (TextView) mUserHeaderView.findViewById(R.id.user_name);
        mAvatar = (ImageView) mUserHeaderView.findViewById(R.id.user_image);
        mJoinDate = (TextView) mUserHeaderView.findViewById(R.id.user_join_date);
        mPostCount = (TextView) mUserHeaderView.findViewById(R.id.post_count);
        mFollowCount = (TextView) mUserHeaderView.findViewById(R.id.watch_count);

        mUserHeaderView.findViewById(R.id.post_count_container).setOnClickListener(this);
        mUserHeaderView.findViewById(R.id.watch_count_container).setOnClickListener(this);

        listView.addHeaderView(mUserHeaderView);
    }

    @Override
    public void notifyDataSetChanged() {
        updateHeaderView();
        super.notifyDataSetChanged();
    }

    private void updateHeaderView() {
        final String userId = ((GetUserPostsTaskMark) getTaskMark()).getUserId();
        UserInfo userInfo = bModule.getCacheManager().getUserInfo(userId);
        if (userInfo != null) {
            setupProfileHeaderView(userInfo);
        }
        QueryUserInfoTaskMark taskMark = new QueryUserInfoTaskMark();
        bModule.getServiceWrapper().queryUserInfo(new IResultReceiver() {
            @Override
            public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
                if (exception != null) {
                    Log.e(TAG, exception.getExMessage());
                } else {
                    UserInfo userInfo;
                    if (mSharedPrefManager.getUserId() != null && mSharedPrefManager.getUserId().equals(userId)) {
                        userInfo = bModule.getCacheManager().getUserInfo();
                    } else {
                        userInfo = bModule.getCacheManager().getUserInfo(userId);
                    }

                    setupProfileHeaderView(userInfo);
                }
            }
        }, taskMark, userId);

    }

    private void setupProfileHeaderView(UserInfo userInfo) {

        if (userInfo == null) {
            return;
        }

        if (mUserName != null) {
            mUserName.setText(userInfo.getName());
        }

        if (mAvatar != null) {
            UserInfo.UserMeta userMeta = userInfo.getUserMeta();
            if (userMeta != null) {
                Picasso.with(mContext).load(userMeta.getAvatar()).transform(new CircleTransform())
                        .error(R.drawable.me_icon_login)
                        .placeholder(R.drawable.me_icon_login)
                        .fit().centerCrop().into(mAvatar);
            }
        }

        if (mJoinDate != null) {
            mJoinDate.setText(mContext.getString(R.string.join_date, AppUtils.getEventPdg(userInfo.getRegisterDate())));
        }

        if (mPostCount != null) {
            mPostCount.setText(userInfo.getUserPostsCount());
        }

        if (mFollowCount != null) {
            mFollowCount.setText(userInfo.getUserPostsWatchingCount());
        }

        if (mContext instanceof ProfileActivity) {
            ((ProfileActivity) mContext).setUsername(userInfo.getName());
        }
    }

    @Override
    public void onClick(View v) {

        ProfileActivity profileActivity = (ProfileActivity) mContext;
        int id = v.getId();

        GetUserPostsTaskMark getUserPostsTaskMark = (GetUserPostsTaskMark) getTaskMark();
        String currentPage = getUserPostsTaskMark.getPage();
        if (id == R.id.post_count_container && !currentPage.equals(GetUserPostsTaskMark.PAGE_HISTORY)) {
            profileActivity.setProfileView(ProfileActivity.HISTORY_VIEW);
        } else if (id == R.id.watch_count_container && !currentPage.equals(GetUserPostsTaskMark.PAGE_FOLLOWING)) {
            profileActivity.setProfileView(ProfileActivity.FOLLOWING_VIEW);
        }
    }

    @Override
    public boolean isLoadDataEmpty() {
        return false;
    }
}
